/**
 * Created by gongxun on 17/7/20.
 */

var GameWsManager = function(){
    var that = {};

    that.node = null;

    that.index = null;

    that.wsList = null;

    that.currentWs = null;

    that.currentHtml = null;

    that.run = function(node){
        that.node = node;
        that.index = 0;

        var ws0 = Math.random() * 10 > 5 ? "104ic61915w7prm1.gfvip02aj.com:10086" : "32www6cr53un3x6f.gfvip02aj.com:10087";
        var ws1 = Math.random() * 10 > 5 ? "t7i179p52j396fy0.gfvip02aj.com:10086" : "021acz9ehto9ir5k.gfvip02aj.com:10087";
        var ws2 = Math.random() * 10 > 5 ? "59ijrvp3oa93os76.gfvip02aj.com:10086" : "343l97b267e0y23o.gfvip02aj.com:10087";

        that.wsList = [
            {"ws" : ws0, "html" : "http://api.qixinxx.com"},
            {"ws" : ws1, "html" : "http://api.qixinxx.com"},
            {"ws" : ws2, "html" : "http://api.qixinxx.com"},
            {"ws" : "47.94.42.88:10086", "html" : "http://47.94.42.88"},
            {"ws" : "47.94.43.188:10086", "html" : "http://47.94.43.188"},
            {"ws" : "47.94.43.213:10086", "html" : "http://47.94.43.213"},
            {"ws" : "47.94.43.235:10086", "html" : "http://47.94.43.235"},
            {"ws" : "47.94.43.30:10086", "html" : "http://47.94.43.30"},
            {"ws" : "47.94.43.202:10086", "html" : "http://47.94.43.202"},
            {"ws" : "47.93.118.212:10086", "html" : "http://47.93.118.212"},
            {"ws" : "47.94.43.233:10086", "html" : "http://47.94.43.233"}
        ];

        that.updateCheck();
    };

    that.updateCheck = function(){
        that.index++;

        var item = that.getCheckItem();
        var wsUrl = item.ws;

        var connectBreak = function(){
            //网络连接失败
            //3秒后重试
            cc.log("网络连接失败 3秒后重试");
            that.node.scheduleOnce(function(){
                that.updateCheck();
            }, 3);
        };

        //1检测网络
        mo.htmlTools.checkNetConnect(function(isSuccess, timeStamp){
            if (isSuccess){
                //检测http
                mo.httpHelper.sendGet(item.html, function(code, xhr){
                    if (code == 0){
                        var client = new WebSocket(wsUrl);
                        client.binaryType = "arraybuffer";

                        client.onopen = function(evt) {
                            cc.log("test websocket onopen" + " index:" + that.index);
                            client.close();

                            that.node.scheduleOnce(function(){
                                that.updateCheck();
                            }, 10);

                            that.onVerifResult(true, item);
                        };

                        client.onmessage = function(evt) {
                            cc.log("test websocket onmessage" + " index:" + that.index);

                        };

                        client.onerror = function(evt) {
                            cc.log("test websocket onerror" + " index:" + that.index);

                            that.node.scheduleOnce(function(){
                                that.updateCheck();
                            }, 1);

                            that.onVerifResult(false, item);
                        };

                        client.onclose = function(evt) {
                            cc.log("test websocket onclose" + " index:" + that.index);

                        };
                    }else{
                        connectBreak();
                        that.getNextWs();
                    }
                });
            }else{
                connectBreak();
            }
        });
    };

    that.getCheckItem = function(){
        return that.wsList[0];
    };

    that.onVerifResult = function(isSuccess, item){
        // mo.gameHelper.websocketIpCur = item.ws;
        // mo.gameHelper.htmlHost = item.html;

        if (isSuccess){
            that.currentWs = item.ws;
            that.currentHtml = item.html;

            mo.gameWsManager.onWsConnect();
        }else{
            that.currentWs = null;
            that.currentHtml = null;
            that.getNextWs();
        }
        that.summary();
    };

    that.getNextWs = function(){
        var frist = that.wsList.shift();
        if (frist.html == "http://api.qixinxx.com"){
            that.wsList.splice(3, 0, frist);
        }else{
            that.wsList.splice(that.wsList.length, 0, frist);
        }
    };

    //连接成功 去找下一个
    that.onWsConnect = function(){
        if (mo.gameWebsocket.mIsConnect){
            var ws = mo.gameHelper.websocketIpCur;
            var html = mo.gameHelper.htmlHost;

            if (ws == that.currentWs && html == that.currentHtml){
                that.getNextWs();
                that.summary();
            }
        }
    };

    //html连接失败 把失败的放到后面去
    that.httpConnectError = function(html){
        if (that.currentHtml == html){
            that.currentWs = null;
            that.currentHtml = null;

            //最多4次可以完成
            for (var i = 0; i < 4; i++){
                if (that.getCheckItem().html == html){
                    that.getNextWs();
                }else{
                    break;
                }
            }
        }

        that.summary();
    };

    //返回html地址
    that.getUserHtml = function(){
        if (!mo.gameWebsocket.mIsWsError){
            //之前的可以用
            return mo.gameHelper.htmlHost;
        }else{
            //之前的不能用了
            return "";
        }
    };

    that.summary = function(){
        cc.log("ws summary");
        cc.log("当前连接html:" + mo.gameHelper.htmlHost);
        cc.log("当前连接ws:" + mo.gameHelper.websocketIpCur);
        cc.log("当前可用html:" + that.currentHtml);
        cc.log("当前可用ws:" + that.currentWs);
        cc.log("排序:");
        that.wsList.forEach(function(cv, index){
            cc.log("index:" + index + " " + cv.ws + " " + cv.html);
        }, that);
    };

    return that;
};

mo.gameWsManager = new GameWsManager();